define(function (require, exports, module) {

    var $ = require('jquery');
    var _ = require('underscore');
    var Base = require('./base');
    var CLS_NAV = 'tabs-nav';
    var CLS_NAV_ACTIVE = 'tabs-nav-active';
    var CLS_NAV_DISABLED = 'tabs-nav-disabled';
    var CLS_CONTENT_ACTIVE = 'tabs-content-active';

    var Tabs = Base.extend({
        attrs: {
            navFather: null,
            navs: null,
            contentFather: null,
            contents: null,
            activeIndex: -1
        },
        initialize: function (navFather, contentFather) {
            var that = this;

            Tabs.superclass.initialize.call(this, {
                navFather: navFather ? $(navFather) : null,
                contentFather: $(contentFather)
            });

            this.set('contents', this.get('contentFather').children('.tabs-content'));

            if (this.get('contents').length <= 0) {
                throw Error('tabs初始化失败');
            }

            if (this.get('navFather')) {
                this.set('navs', this.get('navFather').children('.tabs-nav').filter(function () {
                    return !$(this).hasClass('tabs-nav-disabled');
                }));
                this.set('activeIndex', -1);

                if (this.get('navs').length <= 0 || this.get('navs').length != this.get('contents').length) {
                    throw Error('tabs初始化失败');
                } else {
                    this.get('navFather').delegate('.' + CLS_NAV + ' a', 'click', function (ev) {
                        var navLink = $(this);

                        ev.preventDefault();

                        if (!navLink.parent().hasClass(CLS_NAV_DISABLED)) {
                            that.switchTo(navLink.data('index'));
                        }
                    });
                }
            }
        },
        render: function (activeIndex) {
            var that = this;

            if (!_.isNumber(activeIndex) || activeIndex < -1) {
                activeIndex = 0;
            }

            this.set('activeIndex', activeIndex);

            if (this.get('navFather')) {
                this.get('navs').each(function (index) {
                    var nav = $(this);

                    nav.children('a').data('index', index);

                    if (that.get('activeIndex') == index) {
                        nav.addClass(CLS_NAV_ACTIVE);
                        that.get('contents').eq(index).addClass(CLS_CONTENT_ACTIVE);
                    } else {
                        nav.removeClass(CLS_NAV_ACTIVE);
                        that.get('contents').eq(index).removeClass(CLS_CONTENT_ACTIVE);
                    }
                });
            } else {
                this.get('contents')
                    .removeClass(CLS_CONTENT_ACTIVE)
                    .eq(0).addClass(CLS_CONTENT_ACTIVE);
            }
        },
        switchTo: function (index) {
            var that = this;

            if (_.isNumber(index) && index != this.get('activeIndex')) {
                this.set('activeIndex', index);

                if (this.get('navFather')) {
                    this.get('navs').each(function (indexInNavs) {
                        if (index == indexInNavs) {
                            $(this).addClass(CLS_NAV_ACTIVE);
                            that.get('contents').eq(indexInNavs).addClass(CLS_CONTENT_ACTIVE);
                        } else {
                            $(this).removeClass(CLS_NAV_ACTIVE);
                            that.get('contents').eq(indexInNavs).removeClass(CLS_CONTENT_ACTIVE);
                        }
                    });
                } else {
                    this.get('contents')
                        .removeClass(CLS_CONTENT_ACTIVE)
                        .eq(index).addClass(CLS_CONTENT_ACTIVE);
                }

            }
        }
    });

    /*function Tabs (navFather, contentFather) {
     var that = this;

     this.navFather = $(navFather);
     this.navs = this.navFather.find('.tabs-nav');
     this.contentFather = $(contentFather);
     this.contents = this.contentFather.find('.tabs-content');
     this.activeIndex = -1;

     if (this.navs.length < 0 || this.navs.length != this.contents.length) {
     throw Error('tabs初始化失败');
     }

     this.navFather.delegate('.' + CLS_NAV + ' a', 'click', function (ev) {
     var navLink = $(this);

     ev.preventDefault();

     if (!navLink.parent().hasClass(CLS_NAV_DISABLED)) {
     that.switchTo(navLink.data('index'));
     }
     });
     }

     Tabs.prototype.render = function (activeIndex) {
     var that = this;

     if (!_.isNumber(activeIndex) || activeIndex < 0) {
     activeIndex = 0;
     }

     this.activeIndex = activeIndex;

     this.navs.each(function (index) {
     var nav = $(this);

     nav.children('a').data('index', index);

     if (that.activeIndex == index) {
     nav.addClass(CLS_NAV_ACTIVE);
     that.contents.eq(index).addClass(CLS_CONTENT_ACTIVE);
     } else {
     nav.removeClass(CLS_NAV_ACTIVE);
     that.contents.eq(index).removeClass(CLS_CONTENT_ACTIVE);
     }
     });
     };

     Tabs.prototype.switchTo = function (indexTo) {
     var that = this;

     if (_.isNumber(indexTo) && indexTo != that.activeIndex) {
     that.activeIndex = indexTo;

     this.navs.each(function (index) {
     if (indexTo == index) {
     $(this).addClass(CLS_NAV_ACTIVE);
     that.contents.eq(index).addClass(CLS_CONTENT_ACTIVE);
     } else {
     $(this).removeClass(CLS_NAV_ACTIVE);
     that.contents.eq(index).removeClass(CLS_CONTENT_ACTIVE);
     }
     });
     }
     };*/

    exports.Tabs = Tabs;
});
